﻿using System.ComponentModel.Design;
using System.Security.Cryptography;
using AutoMapper;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using SqlSugar;
using WebApplication1.API返回类;
using WebApplication1.DTO;
using WebApplication1.Models.商品信息.企业;
using WebApplication1.Models.商品信息.地址;
using WebApplication1.Models.商品信息.屠宰;

namespace WebApplication1.Controllers
{
    /// <summary>
    /// 屠宰控制器
    /// </summary>
    [Route("api/[controller]/[action]")]
    [ApiController]
    public class SlaughterController : ControllerBase
    {
        private readonly ISqlSugarClient _db;
        private readonly IMapper mapper;

        public SlaughterController(SqlSugar.ISqlSugarClient db,IMapper mapper)
        {
            this._db = db;
            this.mapper = mapper;
        }

        /// <summary>
        /// 创建屠宰信息
        /// </summary>
        /// <param name="s"></param>
        /// <returns></returns>
        [HttpPost]
        public APIResult<int> CreateSlaughter(SlaughterModel s)
        {
            int r = _db.Insertable(s).ExecuteReturnIdentity();
            if (r > 0)
            {
                return APIResult<int>.Success("创建屠宰成功", r);
            }
            else
            {
                return APIResult<int>.Fail("创建屠宰失败");
            }
        }

        /// <summary>
        /// 查询屠宰信息
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        public APIResult<List<SlaughterModel>> GetEnterpriseAll(int id)
        {
            var list = _db.Queryable<SlaughterModel>().Where(x => x.RId == id).OrderByDescending(x => x.SlaughterId);

            return new APIResult<List<SlaughterModel>>()
            {
                Code = APIEnum.成功,
                Message = "查询屠宰信息成功",
                Data = list.ToList(),
            };
        }

        /// <summary>
        /// 创建屠宰相关资质
        /// </summary>
        /// <param name="s"></param>
        /// <returns></returns>
        [HttpPost]
        public APIResult<int> CreateQualification(QualificationModel s)
        {
            int r = _db.Insertable(s).ExecuteReturnIdentity();
            if (r > 0)
            {
                return APIResult<int>.Success("创建屠宰成功", r);
            }
            else
            {
                return APIResult<int>.Fail("创建屠宰失败");
            }
        }

        /// <summary>
        /// 查询相关资质成功
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        public APIResult<List<QualificationModel>> GetQualificationAll(int id)
        {
            var list = _db.Queryable<QualificationModel>().Where(x => x.RId == id).OrderByDescending(x => x.QualificationId);

            return new APIResult<List<QualificationModel>>()
            {
                Code = APIEnum.成功,
                Message = "查询屠宰相关资质信息成功",
                Data = list.ToList(),
            };
        }

        /// <summary>
        /// 创建屠宰公司
        /// </summary>
        /// <param name="s"></param>
        /// <returns></returns>
        [HttpPost]
        public APIResult<int> CreateCommpany(CommpanyModel s)
        {
            int r = _db.Insertable(s).ExecuteReturnIdentity();
            if (r > 0)
            {
                return APIResult<int>.Success("创建屠宰成功", r);
            }
            else
            {
                return APIResult<int>.Fail("创建屠宰失败");
            }
        }

        /// <summary>
        /// 编辑屠宰公司
        /// </summary>
        /// <param name="s"></param>
        /// <returns></returns>
        [HttpPost]
        public APIResult<int> UpdateCommpany(CommpanyModel s)
        {
            int r = _db.Updateable(s).ExecuteCommand();
            if (r > 0)
            {
                return APIResult<int>.Success("编辑屠宰成功", r);
            }
            else
            {
                return APIResult<int>.Fail("编辑屠宰失败");
            }
        }

        /// <summary>
        /// 查询屠宰公司
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        public APIResult<List<CommpanyDto>> GetCommpanyAll(int id)
        {
            var list = _db.Queryable<CommpanyModel>()
                         .LeftJoin<CommpanyAddressModel>((a, b) => a.CommpanyAddress == b.AddressId)//多个条件用&&
                         .LeftJoin<CityModel>((a,b, c) => c.CityId == b.CityId)//多个条件用&&
                         .LeftJoin<ProvincialModel>((a, b,c,d) => c.ProvincialId == d.ProvincialId)//多个条件用&&
                         .Where(a=>a.RId==id)
                         .Select((a, b,c,d) => new CommpanyDto
                         {
                              CommpanyId        =a.CommpanyId       ,
                              RId               =a. RId             ,
                              CommpanyName      =a. CommpanyName    ,
                              CreateTime        =a. CreateTime      ,
                              CommpanyAddress   =a. CommpanyAddress ,
                              AddressName       = d.ProvincialName + c.CityName + b. AddressName     ,
                              CommpanyDesc      =a. CommpanyDesc    ,
                              ProvincialId      =d.ProvincialId,
                              CityId            =c.CityId,
                         });
            return new APIResult<List<CommpanyDto>>()
            {
                Code = APIEnum.成功,
                Message = "查询屠宰公司信息成功",
                Data = list.ToList(),
            };
        }

        /// <summary>
        /// 创建屠宰公司照片
        /// </summary>
        /// <param name="s"></param>
        /// <returns></returns>
        [HttpPost]
        public APIResult<int> CreateCommpanyPic(CommpanyPicModel s)
        {
            int r = _db.Insertable(s).ExecuteReturnIdentity();
            if (r > 0)
            {
                return APIResult<int>.Success("创建屠宰公司照片成功", r);
            }
            else
            {
                return APIResult<int>.Fail("创建屠宰公司照片失败");
            }
        }

        /// <summary>
        /// 查询屠宰公司照片
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        public APIResult<List<CommpanyPicModel>> GetCommpanyPIcAll(int id)
        {
            var list = _db.Queryable<CommpanyPicModel>().Where(x => x.RId == id).OrderByDescending(x => x.CommpanyPicId);

            return new APIResult<List<CommpanyPicModel>>()
            {
                Code = APIEnum.成功,
                Message = "查询屠宰公司成功",
                Data = list.ToList(),
            };
        }

        
    }
}
